<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 分页样式的抽像类
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
abstract class EGP_Paginator_Style_Abstract
{

    /**
     * 可选参数
     *
     * @var array
     */
    public $params = array();

    /**
     * URI获取当前页页码的关键字
     *
     * @var string
     */
    public $uriSegment = null;

    /**
     * 平均每页显示记录数
     *
     * @var integer
     */
    public $perPage = null;

    /**
     * 记录总数
     *
     * @var integer
     */
    public $itemsCount = null;

    /**
     * 当前页记录总数
     *
     * @var integer
     */
    public $currentItemsCount = null;

    /**
     * 总页数
     *
     * @var integer
     */
    public $pagesCount = null;

    /**
     * 当前页页码
     *
     * @var integer
     */
    public $currentPage = null;

    /**
     * 第一页页码
     *
     * @var integer
     */
    public $firstPage = null;

    /**
     * 最后一页页码
     *
     * @var integer
     */
    public $lastPage = null;

    /**
     * 上一页页码
     *
     * @var integer
     */
    public $previousPage = null;

    /**
     * 下一页页码
     *
     * @var integer
     */
    public $nextPage = null;

    /**
     * 构造方法
     *
     * @param  EGP_Paginator  $paginator
     * @param  array          $params
     */
    public function __construct(EGP_Paginator $paginator, array $params = array())
    {
        $this->params            = $params;
        $this->uriSegment        = $paginator->getUriSegment();
        $this->perPage           = $paginator->getItemsPerPage();
        $this->pagesCount        = $paginator->getPagesCount();
        $this->itemsCount        = $paginator->getItemsCount();
        $this->currentItemsCount = $paginator->getCurrentItemsCount();
        $this->currentPage       = $paginator->getCurrentPage();
        $this->firstPage         = 1;
        $this->lastPage          = $paginator->getPagesCount();
        $this->previousPage      = $this->currentPage - 1;
        if ($this->previousPage < $this->firstPage)
            $this->previousPage  = $this->firstPage;
        $this->nextPage          = $this->currentPage + 1;
        if ($this->nextPage > $this->lastPage)
            $this->nextPage      = $this->lastPage;
    }

    /**
     * 获取页码链接地址
     *
     * @param  integer  $page
     * @return string
     */
    public function getPageLink($page)
    {
        $uri = EGP_Http_Request::getInstance()->getRequestUri();
        $segment = preg_quote($this->uriSegment);
        if (preg_match("/\/$segment\/\d+/i", $uri)) {
            return preg_replace("/(\/$segment)(\/\d+)/i", '\\1/' . $page, $uri);
        } elseif (preg_match("/$segment\=\d+/i", $uri)) {
            return preg_replace("/($segment)(\=\d+)/i", '\\1=' . $page, $uri);
        } else {
            @list($path, $query) = explode('?', $uri);
            return rtrim(rtrim($path, '/') . "/{$this->uriSegment}/$page?$query", '?');
        }
    }

    /**
     * 通过魔术方法输出分页 HTML 代码
     *
     * @return string
     */
    abstract public function __toString();

}